<template>
  <view class="container">
    <tui-toast ref="toast"></tui-toast>
    
    <!-- 顶部导航栏 -->
    <view class="nav-bar">
      <view class="nav-left">
        <view class="back-button" @click="goBack">
          <tui-icon name="arrowleft" size="24" color="#000000"></tui-icon>
        </view>
      </view>
      <view class="nav-title">翻译</view>
      <view class="nav-right">
        <view class="nav-button" @click="toggleHistory">
          <tui-icon name="clock" size="24" color="#555"></tui-icon>
        </view>
      </view>
    </view>
    
    <!-- 语言选择器 -->
    <view class="language-selector-container">
      <view class="language-selector">
        <view class="language-item" @tap="toggleSourceDropdown">
          <text class="language-text">{{ currentSourceLanguage.text }}</text>
          <tui-icon name="arrowdown" :size="16" color="#666666"></tui-icon>
        </view>
        <view class="language-switch" @tap="switchLanguages">
          <tui-icon name="turnaround" size="20" color="#5A87FE"></tui-icon>
        </view>
        <view class="language-item" @tap="toggleTargetDropdown">
          <text class="language-text">{{ currentTargetLanguage.text }}</text>
          <tui-icon name="arrowdown" :size="16" color="#666666"></tui-icon>
        </view>
      </view>
    </view>
    
    <!-- 下拉菜单 -->
    <view class="dropdown-container" v-if="showSourceDropdown || showTargetDropdown">
      <view class="dropdown-overlay" @tap="closeDropdowns"></view>
      <view class="dropdown-menu" :class="{ 'source-dropdown': showSourceDropdown, 'target-dropdown': showTargetDropdown }">
        <scroll-view scroll-y class="dropdown-list">
          <view
            class="dropdown-item"
            v-for="lang in (showSourceDropdown ? availableSourceLanguages : availableTargetLanguages)"
            :key="lang.code"
            :class="{ active: (showSourceDropdown ? currentSource : currentTarget) === lang.code }"
            @tap="selectLanguage(lang.code)"
          >
            <text>{{ lang.text }}</text>
          </view>
        </scroll-view>
      </view>
    </view>
    
    <!-- 可滚动区域 -->
    <scroll-view 
      class="scrollable-content"
      scroll-y
      refresher-enabled
      :refresher-triggered="isRefreshing"
      @refresherrefresh="onRefresh"
      @refresherrestore="onRestoreRefresh"
    >
      <!-- 主翻译区域 -->
      <view class="translation-box">
        <!-- 输入区域 -->
        <view class="input-box">
          <textarea 
            class="input-text" 
            v-model="sourceText" 
            placeholder="请输入要翻译的文本" 
            auto-height
            @focus="closeDropdowns"
          ></textarea>
          <view class="clear-button" v-if="sourceText" @click="clearText">
            <tui-icon name="close-fill" size="16" color="#999999"></tui-icon>
          </view>
        </view>
        
        <!-- 输出区域 -->
        <view class="output-box">
          <view v-if="isLoading" class="loading-box">
            <tui-loading type="circle" color="#5A87FE"></tui-loading>
            <text class="loading-text">翻译中...</text>
          </view>
          <view v-else-if="targetText" class="result-box">
            <text class="result-text">{{ targetText }}</text>
            <view class="copy-button" @click="copyResult">
              <tui-icon name="copy" size="18" color="#5A87FE"></tui-icon>
            </view>
          </view>
          <view v-else class="empty-box">
            <text class="empty-text">翻译结果将显示在这里</text>
          </view>
        </view>
      </view>
      
      <!-- 底部占位，防止内容被工具栏遮挡 -->
      <view class="bottom-space"></view>
    </scroll-view>
    
    <!-- 底部工具栏 -->
    <view class="tool-bar">
      <tui-button 
        type="primary" 
        width="100%"
        height="88rpx"
        radius="44rpx"
        :loading="isLoading"
        :disabled="!sourceText.trim()"
        @click="translate"
        background="linear-gradient(135deg, #5A87FE 0%, #6E9AFF 100%)"
      >
        翻译
      </tui-button>
    </view>
    
    <!-- 历史记录抽屉 -->
    <tui-drawer mode="bottom" :visible="showHistory" @close="showHistory = false" radius="16">
      <view class="history-drawer">
        <view class="drawer-header">
          <text class="drawer-title">翻译历史</text>
          <view class="drawer-close" @click="showHistory = false">
            <tui-icon name="close" size="20" color="#999999"></tui-icon>
          </view>
        </view>
        
        <scroll-view scroll-y class="history-list" v-if="history.length > 0">
          <view class="history-item" v-for="item in history" :key="item.id">
            <view class="history-languages">
              <text class="history-language">{{ item.sourceLanguage }} → {{ item.targetLanguage }}</text>
              <text class="history-time">{{ item.timestamp }}</text>
            </view>
            <view class="history-content">
              <text class="history-source-text">{{ item.sourceText }}</text>
              <text class="history-target-text">{{ item.targetText }}</text>
            </view>
          </view>
        </scroll-view>
        
        <view class="empty-history" v-else>
          <text>暂无翻译历史</text>
        </view>
      </view>
    </tui-drawer>
  </view>
</template>

<script>
export default {
  data() {
    return {
      sourceText: '',
      targetText: '',
      isLoading: false,
      isRefreshing: false,
      showHistory: false,
      showSourceDropdown: false,
      showTargetDropdown: false,
      currentSource: 'zh',
      currentTarget: 'ug',
      languages: [
        { code: 'zh', text: '中文' },
        { code: 'ug', text: '维文' },
        { code: 'kk', text: '哈文' }
      ],
      history: []
    }
  },
  computed: {
    currentSourceLanguage() {
      return this.languages.find(lang => lang.code === this.currentSource) || this.languages[0]
    },
    currentTargetLanguage() {
      return this.languages.find(lang => lang.code === this.currentTarget) || this.languages[1]
    },
    availableSourceLanguages() {
      return this.languages.filter(lang => lang.code !== this.currentTarget)
    },
    availableTargetLanguages() {
      return this.languages.filter(lang => lang.code !== this.currentSource)
    }
  },
  methods: {
    toggleSourceDropdown() {
      this.showSourceDropdown = !this.showSourceDropdown
      this.showTargetDropdown = false
    },
    toggleTargetDropdown() {
      this.showTargetDropdown = !this.showTargetDropdown
      this.showSourceDropdown = false
    },
    closeDropdowns() {
      this.showSourceDropdown = false
      this.showTargetDropdown = false
    },
    selectLanguage(code) {
      if (this.showSourceDropdown) {
        this.currentSource = code
        this.showSourceDropdown = false
      } else if (this.showTargetDropdown) {
        this.currentTarget = code
        this.showTargetDropdown = false
      }
    },
    switchLanguages() {
      const temp = this.currentSource
      this.currentSource = this.currentTarget
      this.currentTarget = temp
      const tempText = this.sourceText
      this.sourceText = this.targetText
      this.targetText = tempText
    },
    toggleHistory() {
      this.showHistory = !this.showHistory
    },
    clearText() {
      this.sourceText = ''
      this.targetText = ''
    },
    copyResult() {
      if (this.targetText) {
        uni.setClipboardData({
          data: this.targetText,
          success: () => {
            this.$refs.toast.show({
              title: '复制成功',
              type: 'success'
            })
          }
        })
      }
    },
    translate() {
      if (!this.sourceText.trim()) return
      
      this.isLoading = true
      // 模拟翻译过程
      setTimeout(() => {
        let result = ''
        if (this.currentSource === 'zh' && this.currentTarget === 'ug') {
          result = '这是中文到维文的翻译结果'
        } else if (this.currentSource === 'ug' && this.currentTarget === 'zh') {
          result = '这是维文到中文的翻译结果'
        } else if (this.currentSource === 'zh' && this.currentTarget === 'kk') {
          result = '这是中文到哈文的翻译结果'
        } else if (this.currentSource === 'kk' && this.currentTarget === 'zh') {
          result = '这是哈文到中文的翻译结果'
        } else if (this.currentSource === 'ug' && this.currentTarget === 'kk') {
          result = '这是维文到哈文的翻译结果'
        } else if (this.currentSource === 'kk' && this.currentTarget === 'ug') {
          result = '这是哈文到维文的翻译结果'
        }
        
        this.targetText = result
        this.isLoading = false
        this.addToHistory()
      }, 1000)
    },
    addToHistory() {
      this.history.unshift({
        id: Date.now(),
        sourceLanguage: this.currentSourceLanguage.text,
        targetLanguage: this.currentTargetLanguage.text,
        sourceText: this.sourceText,
        targetText: this.targetText,
        timestamp: new Date().toLocaleString()
      })
      
      // 限制历史记录数量
      if (this.history.length > 10) {
        this.history.pop()
      }
    },
    goBack() {
      uni.navigateBack()
    },
    async onRefresh() {
      this.isRefreshing = true
      try {
        await this.translate()
      } finally {
        this.isRefreshing = false
      }
    },
    onRestoreRefresh() {
      this.isRefreshing = false
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
}

/* 顶部导航栏 */
.nav-bar {
  padding: 88rpx 32rpx 32rpx;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1rpx solid rgba(0, 0, 0, 0.03);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.03);
}

.nav-left, .nav-right {
  width: 80rpx;
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #1a1a1a;
  letter-spacing: 0.5rpx;
}

.back-button, .nav-button {
  width: 64rpx;
  height: 64rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 32rpx;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.back-button:active, .nav-button:active {
  background-color: rgba(0, 0, 0, 0.05);
  transform: scale(0.95);
}

/* 语言选择器 */
.language-selector-container {
  margin-top: 180rpx;
  padding: 24rpx 32rpx;
  position: relative;
  z-index: 99;
}

.language-selector {
  background: #ffffff;
  border-radius: 28rpx;
  padding: 16rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  border: 1rpx solid rgba(90, 135, 254, 0.1);
}

.language-item {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8rpx;
  padding: 16rpx 24rpx;
  background: rgba(90, 135, 254, 0.05);
  border-radius: 20rpx;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.language-item:active {
  background: rgba(90, 135, 254, 0.1);
  transform: scale(0.98);
}

.language-text {
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
}

.language-switch {
  width: 72rpx;
  height: 72rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #5A87FE 0%, #6E9AFF 100%);
  border-radius: 36rpx;
  margin: 0 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(90, 135, 254, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.language-switch:active {
  transform: scale(0.9) rotate(180deg);
  box-shadow: 0 2rpx 8rpx rgba(90, 135, 254, 0.15);
}

/* 下拉菜单 */
.dropdown-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 101;
}

.dropdown-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(4px);
  animation: fade-in 0.3s ease;
}

.dropdown-menu {
  position: absolute;
  left: 32rpx;
  right: 32rpx;
  background: #ffffff;
  border-radius: 28rpx;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.1);
  overflow: hidden;
  animation: slide-in 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateY(-20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.dropdown-list {
  max-height: 400rpx;
  padding: 16rpx 0;
}

.dropdown-item {
  padding: 24rpx 32rpx;
  font-size: 28rpx;
  color: #333;
  display: flex;
  align-items: center;
  transition: all 0.2s ease;
}

.dropdown-item.active {
  color: #5A87FE;
  background: rgba(90, 135, 254, 0.08);
  font-weight: 500;
}

.dropdown-item:active {
  background: rgba(90, 135, 254, 0.05);
}

/* 可滚动区域 */
.scrollable-content {
  position: fixed;
  top: 300rpx;
  left: 0;
  right: 0;
  bottom: 168rpx;
  background: transparent;
}

/* 主翻译区域 */
.translation-box {
  margin: 32rpx;
  display: flex;
  flex-direction: column;
  gap: 24rpx;
}

.input-box, .output-box {
  background: #ffffff;
  border-radius: 28rpx;
  padding: 32rpx;
  position: relative;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  border: 1rpx solid rgba(90, 135, 254, 0.1);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.input-box:focus-within {
  border-color: rgba(90, 135, 254, 0.3);
  box-shadow: 0 8rpx 24rpx rgba(90, 135, 254, 0.1);
  transform: translateY(-2rpx);
}

.input-text {
  width: 100%;
  min-height: 200rpx;
  font-size: 32rpx;
  line-height: 1.6;
  color: #333;
}

.clear-button {
  position: absolute;
  right: 16rpx;
  top: 16rpx;
  width: 48rpx;
  height: 48rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 24rpx;
  transition: all 0.3s ease;
}

.clear-button:active {
  background: rgba(0, 0, 0, 0.08);
  transform: scale(0.9);
}

.loading-box {
  min-height: 200rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 16rpx;
}

.loading-text {
  font-size: 28rpx;
  color: #999;
  letter-spacing: 0.5rpx;
}

.result-box {
  position: relative;
  min-height: 200rpx;
}

.result-text {
  font-size: 32rpx;
  line-height: 1.6;
  color: #333;
  padding-right: 64rpx;
}

.copy-button {
  position: absolute;
  right: 16rpx;
  top: 16rpx;
  width: 48rpx;
  height: 48rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(90, 135, 254, 0.08);
  border-radius: 24rpx;
  transition: all 0.3s ease;
}

.copy-button:active {
  background: rgba(90, 135, 254, 0.15);
  transform: scale(0.9);
}

.empty-box {
  min-height: 200rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  letter-spacing: 0.5rpx;
}

/* 底部占位 */
.bottom-space {
  height: 32rpx;
}

/* 底部工具栏 */
.tool-bar {
  padding: 32rpx;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1rpx solid rgba(0, 0, 0, 0.03);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.03);
}

/* 历史记录抽屉 */
.history-drawer {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top-left-radius: 36rpx;
  border-top-right-radius: 36rpx;
  min-height: 60vh;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -4rpx 24rpx rgba(0, 0, 0, 0.05);
}

.drawer-header {
  padding: 32rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1rpx solid rgba(0, 0, 0, 0.03);
}

.drawer-title {
  font-size: 34rpx;
  font-weight: 600;
  color: #333;
  letter-spacing: 0.5rpx;
}

.drawer-close {
  width: 64rpx;
  height: 64rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 32rpx;
  transition: all 0.3s ease;
}

.drawer-close:active {
  background: rgba(0, 0, 0, 0.05);
  transform: scale(0.95);
}

.history-list {
  flex: 1;
  padding: 0 32rpx;
}

.history-item {
  padding: 32rpx 0;
  border-bottom: 1rpx solid rgba(0, 0, 0, 0.03);
  transition: all 0.3s ease;
}

.history-item:active {
  background: rgba(0, 0, 0, 0.02);
  transform: scale(0.99);
}

.history-languages {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
}

.history-language {
  font-size: 24rpx;
  color: #5A87FE;
  background: rgba(90, 135, 254, 0.08);
  padding: 8rpx 24rpx;
  border-radius: 24rpx;
  font-weight: 500;
}

.history-time {
  font-size: 24rpx;
  color: #999;
}

.history-content {
  display: flex;
  flex-direction: column;
  gap: 12rpx;
}

.history-source-text {
  font-size: 28rpx;
  color: #666;
  line-height: 1.6;
}

.history-target-text {
  font-size: 28rpx;
  color: #333;
  line-height: 1.6;
  font-weight: 500;
}

.empty-history {
  padding: 100rpx 0;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #999;
  font-size: 28rpx;
  letter-spacing: 0.5rpx;
}
</style>